﻿using Domain.IRepositorys;
using Domain.Model;
using Domain.VO;
using Infrastructure.RepositoryBase;
using Infrastructure.UniOfWorkBase;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Repository
{
    public class InComeWalletLogRepository : AbstractRepository<InComeWalletLogAggregate>, IInComeWalletLogRepository
    {
        public List<FirendVO> GetFirendsList(int uid, string[] types, int page, int size)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var item in types)
            {
                if (stringBuilder.Length > 0) stringBuilder.Append(",");
                stringBuilder.Append("'");
                stringBuilder.Append(item);
                stringBuilder.Append("'");
            }
            var startPage = page <= 0 ? 0 : page - 1;

            var sql = $"select FromUid,SUM(ChangeMoney) TotalMoney,MAX(CreateTime) CT from walletlog WHERE UserID={uid} and BizType in ({stringBuilder.ToString()}) and LogType='InCome' GROUP BY FromUid ORDER BY CT DESC LIMIT {startPage*size},{size}";

            var list = dbContext.Database.SqlQuery<FirendVO>(sql).ToList();

            return list;
        }
    }
}
